<?php

Loader::using("ext::libs::restful::RestfulServiceConsumer");

/**
 * Description of Authentication
 *
 * @author ETorres
 */
class Authentication extends ModuleModel {

    const tabla = "acaweb_usuarios";

    /**
     *
     * @var Query
     */
    private $query;

    function __construct() {
        parent::__construct();
        $this->query = ModulesContext::instance()->getQueryFactory()->getQuery();
    }

    function doLogin($user) {
        $token = ModulesContext::instance()->getConfig()->get("loginToken");
        
        $data = array(
            'usuario' => $user['username'],
            'clave'   => $user['password'],
            'token'   => $token
        );
        
//      Estudiante
//        $data = array(
//            'usuario' => '1055899',
//            'clave'   => 'Est1047421699',
//            'token'   => $token
//        );

//      Egresado
//        $data = array(
//            'usuario' => '1047421699E',
//            'clave'   => '1047421699E',
//            'token'   => $token
//        );
        
//        Docente de pruebas
//        $data = array(
//            'usuario' => '45439680P',
//            'clave'   => '45439680P',
//            'token'   => $token
//        );
        
        $client = new RestfulServiceConsumer('http://online.usbctg.edu.co:81/admon/');
        
        $response = $client->invokeService('Login', array(
                    "serviceClass" => 'WebUtilities.asmx',
                    "contentType" => "application/json",
                    "acceptType" => "application/json",
                    "method" => "POST",
                    "data" => json_encode($data),
                ));
        
        $result = json_decode($response, true);
        
        if($result != null){
            $_SESSION['user_logged_in'] = true;
            $_SESSION['session_token'] = $token;
            $_SESSION['current_user'] = $result['Codigo'];
            $_SESSION['current_user_roles'] = $result['Rol'];
            $_SESSION['current_user_programas'] = $result['Programa'];
            
            return TRUE;
        } else {
            $this->doLogout();
            
            return FALSE;
        }
        
        /*
         * Metodo de usuarios locales en acaweb_usuarios
         
        $result = $this->query->select()
                ->from(array(self::tabla))
                ->where("username", $user['username'])
                ->where("password", md5($user['password']))
                ->get();

        if ($result->firstRow()) {
            $_SESSION['user_logged_in'] = true;
            $_SESSION['session_token'] = $token;
        } else {
            $_SESSION['user_logged_in'] = false;
            $_SESSION['session_token'] = null;
        }
         
         */
    }

    function doLogout() {
        $_SESSION['user_logged_in'] = FALSE;
        $_SESSION['session_token'] = NULL;
        $_SESSION['current_user'] = NULL;
        $_SESSION['current_user_roles'] = NULL;
        $_SESSION['current_user_programas'] = NULL;
    }
    
    function isAuthenticated() {
        $token = ModulesContext::instance()->getConfig()->get("loginToken");
        
        if ($_SESSION['user_logged_in'] && $_SESSION['session_token'] == $token){
            return TRUE;
        } else {
            return FALSE;
        }
    }

}

?>
